# -*- coding: utf-8 -*-
from PIL import Image, ImageFile
from StringIO import StringIO
import glob

if __name__ == '__main__':
    size = 120
    file_list = glob.glob('/home/me/图片/t*.jpg')

    print file_list
    base_path = file_list[0]
#    orig = open(base_path, 'rb').read()
#    p = ImageFile.Parser()
#    p.feed(orig)
    try:
        image = Image.open('/home/me/a.gif')
    except IOError:
        print 'IOError!!!'
#    try:
##        image = p.close()
#        image.show()
#    except IOError:
#        #return # What should we do here?  Render a "sorry, didn't work" img?
#        pass
    thumb = StringIO()
    if image.mode != "RGB":
        image = image.convert("RGB")
    try:
        image.save(thumb, "JPEG")
    except:
        print 'image save Error'
#    thumb_file = ContentFile(thumb.getvalue())
#    dist_file = self.avatar_file(size)
    (w, h) = image.size
    if w > h:
        diff = (w - h) / 2
        image = image.crop((diff, 0, w - diff, h))
    else:
        diff = (h - w) / 2
        image = image.crop((0, diff, w, h - diff))
    image = image.resize((size, size))
    image.show()
